#pragma once
#include <iostream>
#include <functional>
#include <string>
#include <pthread.h>
using func_t = std::function<void()>;
class Thread
{
        void Execute()
        {
            _task();
        }
    public:
        Thread( std::string threadname , func_t task)
        :_threadname(threadname)
        ,_task(task)
        {}
        static void* threadroutine(void* args)
        {
            Thread* self = static_cast<Thread*>(args);
            self->Execute();
            return nullptr;
        }
        void Start()
        {
            pthread_create(&_tid,nullptr,threadroutine,this);
        }
        void Join()
        {
            int n = pthread_join(_tid,nullptr);
            if(n != 0)
            {
                printf("Thread Join Error!\n");
                return;
            }
        }
        void Detach()
        {
            int n = pthread_detach(_tid);
            if(n != 0)
            {
                printf("set pthread detach error!\n");
                return;
            }
        }
        std::string threadname()
        {
            return _threadname;
        }
    private:
        pthread_t _tid;
        std::string _threadname;
        func_t _task;
};